package my.diplom.util;

import my.diplom.entity.Resource;
import my.diplom.entity.Project;
import my.diplom.entity.Event;

import java.util.List;
import java.util.Iterator;
import java.util.Locale;
import java.text.SimpleDateFormat;
import java.sql.Date;

/**
 * Created by IntelliJ IDEA.
 * User: gb
 * Date: 25.10.2009
 * Time: 16:57:12
 * To change this template use File | Settings | File Templates.
 */
public class JsonUtil {

    private static SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM d yyyy HH:mm:ss Z", Locale.US);
    private static String[] colors = {"red", "green", "blue", "gray", "black", "orange", "white", "black", "pink"};

    public static String resourcesToJson(List<Resource> resources) {
        String json = "{\"wiki-url\":\"http://simile.mit.edu/shelf/\", \"wiki-section\":\"Simile JFK Timeline\", \"dateTimeFormat\": \"Gregorian\"," +
                "\"events\": [";
        int i = 0;
        int trackNum = 1;
        for (Iterator iterator = resources.iterator(); iterator.hasNext();) {
            if (i == colors.length) i = 0;
            Resource r = (Resource) iterator.next();

            String s = "<strong>Involved projects:</strong><br>";
            for (Iterator iter = r.getProjects().iterator(); iter.hasNext();) {
                Project p = (Project) iter.next();
                s += p.getProjectName() + "<br>";
            }

           

            json += "{\"start\":\"" + dateFormat.format(r.getStartDate()) + "\"," +
                    "\"end\":\"" + dateFormat.format(r.getFinishDate()) + "\"," +
                    "\"trackNum\":\"" + trackNum + "\"," +
                    "\"title\":\"" + r.getHostName() + "\"," +
                    "\"description\":\"" + r.getDescription() + ' ' + r.getIpAddress() + "<br/>" +
                    "<strong>CPU:</strong>" + r.getCpu() + " GHz<br/>" +
                    "<strong>RAM:</strong>" + r.getRam() + " GB<br/>" +
                    "<strong>HDD:</strong>" + r.getHdd() + " GB<br/>" +
                    s +
                    "\"," +
                    "\"color\":\"" + colors[i] + "\"," +
                    //"\"classname\":" +"\"my_event\"," +
                    //"\"color\":" +"\"red\"," +
                    "\"durationEvent\":true}";

            i++;


            if (r.getEvents().size() > 0) {
                for (Event e : r.getEvents()) {
                    json += ",{\"start\":\"" + dateFormat.format(e.getStartDate()) + "\"," +
                            "\"end\":\"" + dateFormat.format(e.getFinishDate()) + "\"," +
                            "\"trackNum\":\"" + trackNum + "\"," +
                            "\"title\":\"" + e.getEventName() + "\"," +
                            "\"description\":\"" + e.getDescription() + "\"," +
                            "\"color\":\"" + colors[i] + "\"," +
                            "\"isChild\":" + "\"true\"," +
                            "\"classname\":" + "\"my-event\"," +
                            "\"durationEvent\":true}";
                    i++;

                }

            }
            trackNum++;

            if (iterator.hasNext()) json += ",";


        }
        json += "]}";

        System.out.println(json);
        return json;
    }

}
